package com.tanhua.manage.controller;

import com.tanhua.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 接口名称：用户数据翻页
     * 接口路径：GET/manage/users
     */
    @GetMapping("users")
    public ResponseEntity<Object> findByPage(Integer page, Integer pagesize) {
        return userService.findByPage(page, pagesize);
    }
    
    /**
     * 接口名称：用户基本资料
     * 接口路径：GET/manage/users/:userID
     */
    @GetMapping("users/{userId}")
    public ResponseEntity<Object> findUserInfoById(@PathVariable("userId") Long userId) {
        return userService.findUserInfoById(userId);
    }
    
    /**
     * 接口名称：视频记录翻页
     * 接口路径：GET/manage/videos
     */
    @GetMapping("videos")
    public ResponseEntity<Object> findVideosList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize, Long uid) {
        return userService.findVideosList(page, pagesize, uid);
    }
    
    /**
     * 接口名称：动态分页
     * 接口路径：GET/manage/messages
     */
    @GetMapping("messages")
    public ResponseEntity<Object> findMovementsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize, Long uid, String state) {
        return userService.findMovementsList(page, pagesize, uid, state);
    }
    
    /**
     * 接口名称：动态详情
     * 接口路径：GET/manage/messages/:id
     */
    @GetMapping("messages/{id}")
    public ResponseEntity<Object> findMovementsById(
            @PathVariable("id") String publishId) {
        return userService.findMovementsById(publishId);
    }
    
    /**
     * 接口名称：评论列表翻页
     * 接口路径：GET/manage/messages/comments
     */
    @GetMapping("messages/comments")
    public ResponseEntity<Object> commentsList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pagesize, String messageID) {
        return userService.commentsList(page, pagesize, messageID);
    }
    
    /**
     * 接口名称：动态通过
     * 接口路径：POST/manage/messages/pass
     */
    @PostMapping("messages/pass")
    public ResponseEntity<Object> pass(@RequestBody String[] publishIds) {
        return userService.pass(publishIds);
    }
    
    /**
     * 接口名称：动态通过
     * 接口路径：POST/manage/messages/reject
     */
    @PostMapping("messages/reject")
    public ResponseEntity<Object> reject(@RequestBody String[] publishIds) {
        return userService.reject(publishIds);
    }
    
    /**
     * 用户冻结操作
     * POST/manage/users/freeze
     *
     * @param param 封装了用户id，冻结时间，冻结范围，冻结原因，冻结备注
     * @return
     */
    @PostMapping("users/freeze")
    public ResponseEntity<Object> freeze(@RequestBody Map<String, String> param) {
        //从集合里获取用户id(可以创建一个对象来接收这些数据)
        Integer userId = Integer.valueOf(param.get("userId"));
        
        //从集合里获取冻结时间
        Integer freezingTime = Integer.valueOf(param.get("freezingTime"));
        //从集合里获取冻结范围
        Integer freezingRange = Integer.valueOf(param.get("freezingRange"));
        //从集合里获取冻结原因
        String reasonsForFreezing = (String) param.get("reasonsForFreezing");
        //冻结备注
        String frozenRemarks = (String) param.get("frozenRemarks");
        return userService.freeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
    }
    
    /**
     * 用户解冻操作
     * POST/manage/users/unfreeze
     *
     * @param param 封装了用户id，解封原因
     * @return
     */
    @PostMapping("users/unfreeze")
    public ResponseEntity<Object> unfreeze(@RequestBody Map<String, String> param) {
        //从集合里取出用户id
        Long userId = Long.valueOf(param.get("userId"));
        return userService.unfreeze(userId);
    }
}
